// Code generated by ent, DO NOT EDIT.

package ent

import (
	"context"
	"errors"
	"fmt"
	"server/components/models/ent/regiontype"

	"entgo.io/ent/dialect/sql"
	"entgo.io/ent/dialect/sql/sqlgraph"
	"entgo.io/ent/schema/field"
)

// RegionTypeCreate is the builder for creating a RegionType entity.
type RegionTypeCreate struct {
	config
	mutation *RegionTypeMutation
	hooks    []Hook
	conflict []sql.ConflictOption
}

// SetType sets the "type" field.
func (rtc *RegionTypeCreate) SetType(s string) *RegionTypeCreate {
	rtc.mutation.SetType(s)
	return rtc
}

// SetID sets the "id" field.
func (rtc *RegionTypeCreate) SetID(i int64) *RegionTypeCreate {
	rtc.mutation.SetID(i)
	return rtc
}

// Mutation returns the RegionTypeMutation object of the builder.
func (rtc *RegionTypeCreate) Mutation() *RegionTypeMutation {
	return rtc.mutation
}

// Save creates the RegionType in the database.
func (rtc *RegionTypeCreate) Save(ctx context.Context) (*RegionType, error) {
	return withHooks(ctx, rtc.sqlSave, rtc.mutation, rtc.hooks)
}

// SaveX calls Save and panics if Save returns an error.
func (rtc *RegionTypeCreate) SaveX(ctx context.Context) *RegionType {
	v, err := rtc.Save(ctx)
	if err != nil {
		panic(err)
	}
	return v
}

// Exec executes the query.
func (rtc *RegionTypeCreate) Exec(ctx context.Context) error {
	_, err := rtc.Save(ctx)
	return err
}

// ExecX is like Exec, but panics if an error occurs.
func (rtc *RegionTypeCreate) ExecX(ctx context.Context) {
	if err := rtc.Exec(ctx); err != nil {
		panic(err)
	}
}

// check runs all checks and user-defined validators on the builder.
func (rtc *RegionTypeCreate) check() error {
	if _, ok := rtc.mutation.GetType(); !ok {
		return &ValidationError{Name: "type", err: errors.New(`ent: missing required field "RegionType.type"`)}
	}
	return nil
}

func (rtc *RegionTypeCreate) sqlSave(ctx context.Context) (*RegionType, error) {
	if err := rtc.check(); err != nil {
		return nil, err
	}
	_node, _spec := rtc.createSpec()
	if err := sqlgraph.CreateNode(ctx, rtc.driver, _spec); err != nil {
		if sqlgraph.IsConstraintError(err) {
			err = &ConstraintError{msg: err.Error(), wrap: err}
		}
		return nil, err
	}
	if _spec.ID.Value != _node.ID {
		id := _spec.ID.Value.(int64)
		_node.ID = int64(id)
	}
	rtc.mutation.id = &_node.ID
	rtc.mutation.done = true
	return _node, nil
}

func (rtc *RegionTypeCreate) createSpec() (*RegionType, *sqlgraph.CreateSpec) {
	var (
		_node = &RegionType{config: rtc.config}
		_spec = sqlgraph.NewCreateSpec(regiontype.Table, sqlgraph.NewFieldSpec(regiontype.FieldID, field.TypeInt64))
	)
	_spec.OnConflict = rtc.conflict
	if id, ok := rtc.mutation.ID(); ok {
		_node.ID = id
		_spec.ID.Value = id
	}
	if value, ok := rtc.mutation.GetType(); ok {
		_spec.SetField(regiontype.FieldType, field.TypeString, value)
		_node.Type = value
	}
	return _node, _spec
}

// OnConflict allows configuring the `ON CONFLICT` / `ON DUPLICATE KEY` clause
// of the `INSERT` statement. For example:
//
//	client.RegionType.Create().
//		SetType(v).
//		OnConflict(
//			// Update the row with the new values
//			// the was proposed for insertion.
//			sql.ResolveWithNewValues(),
//		).
//		// Override some of the fields with custom
//		// update values.
//		Update(func(u *ent.RegionTypeUpsert) {
//			SetType(v+v).
//		}).
//		Exec(ctx)
func (rtc *RegionTypeCreate) OnConflict(opts ...sql.ConflictOption) *RegionTypeUpsertOne {
	rtc.conflict = opts
	return &RegionTypeUpsertOne{
		create: rtc,
	}
}

// OnConflictColumns calls `OnConflict` and configures the columns
// as conflict target. Using this option is equivalent to using:
//
//	client.RegionType.Create().
//		OnConflict(sql.ConflictColumns(columns...)).
//		Exec(ctx)
func (rtc *RegionTypeCreate) OnConflictColumns(columns ...string) *RegionTypeUpsertOne {
	rtc.conflict = append(rtc.conflict, sql.ConflictColumns(columns...))
	return &RegionTypeUpsertOne{
		create: rtc,
	}
}

type (
	// RegionTypeUpsertOne is the builder for "upsert"-ing
	//  one RegionType node.
	RegionTypeUpsertOne struct {
		create *RegionTypeCreate
	}

	// RegionTypeUpsert is the "OnConflict" setter.
	RegionTypeUpsert struct {
		*sql.UpdateSet
	}
)

// SetType sets the "type" field.
func (u *RegionTypeUpsert) SetType(v string) *RegionTypeUpsert {
	u.Set(regiontype.FieldType, v)
	return u
}

// UpdateType sets the "type" field to the value that was provided on create.
func (u *RegionTypeUpsert) UpdateType() *RegionTypeUpsert {
	u.SetExcluded(regiontype.FieldType)
	return u
}

// UpdateNewValues updates the mutable fields using the new values that were set on create except the ID field.
// Using this option is equivalent to using:
//
//	client.RegionType.Create().
//		OnConflict(
//			sql.ResolveWithNewValues(),
//			sql.ResolveWith(func(u *sql.UpdateSet) {
//				u.SetIgnore(regiontype.FieldID)
//			}),
//		).
//		Exec(ctx)
func (u *RegionTypeUpsertOne) UpdateNewValues() *RegionTypeUpsertOne {
	u.create.conflict = append(u.create.conflict, sql.ResolveWithNewValues())
	u.create.conflict = append(u.create.conflict, sql.ResolveWith(func(s *sql.UpdateSet) {
		if _, exists := u.create.mutation.ID(); exists {
			s.SetIgnore(regiontype.FieldID)
		}
	}))
	return u
}

// Ignore sets each column to itself in case of conflict.
// Using this option is equivalent to using:
//
//	client.RegionType.Create().
//	    OnConflict(sql.ResolveWithIgnore()).
//	    Exec(ctx)
func (u *RegionTypeUpsertOne) Ignore() *RegionTypeUpsertOne {
	u.create.conflict = append(u.create.conflict, sql.ResolveWithIgnore())
	return u
}

// DoNothing configures the conflict_action to `DO NOTHING`.
// Supported only by SQLite and PostgreSQL.
func (u *RegionTypeUpsertOne) DoNothing() *RegionTypeUpsertOne {
	u.create.conflict = append(u.create.conflict, sql.DoNothing())
	return u
}

// Update allows overriding fields `UPDATE` values. See the RegionTypeCreate.OnConflict
// documentation for more info.
func (u *RegionTypeUpsertOne) Update(set func(*RegionTypeUpsert)) *RegionTypeUpsertOne {
	u.create.conflict = append(u.create.conflict, sql.ResolveWith(func(update *sql.UpdateSet) {
		set(&RegionTypeUpsert{UpdateSet: update})
	}))
	return u
}

// SetType sets the "type" field.
func (u *RegionTypeUpsertOne) SetType(v string) *RegionTypeUpsertOne {
	return u.Update(func(s *RegionTypeUpsert) {
		s.SetType(v)
	})
}

// UpdateType sets the "type" field to the value that was provided on create.
func (u *RegionTypeUpsertOne) UpdateType() *RegionTypeUpsertOne {
	return u.Update(func(s *RegionTypeUpsert) {
		s.UpdateType()
	})
}

// Exec executes the query.
func (u *RegionTypeUpsertOne) Exec(ctx context.Context) error {
	if len(u.create.conflict) == 0 {
		return errors.New("ent: missing options for RegionTypeCreate.OnConflict")
	}
	return u.create.Exec(ctx)
}

// ExecX is like Exec, but panics if an error occurs.
func (u *RegionTypeUpsertOne) ExecX(ctx context.Context) {
	if err := u.create.Exec(ctx); err != nil {
		panic(err)
	}
}

// Exec executes the UPSERT query and returns the inserted/updated ID.
func (u *RegionTypeUpsertOne) ID(ctx context.Context) (id int64, err error) {
	node, err := u.create.Save(ctx)
	if err != nil {
		return id, err
	}
	return node.ID, nil
}

// IDX is like ID, but panics if an error occurs.
func (u *RegionTypeUpsertOne) IDX(ctx context.Context) int64 {
	id, err := u.ID(ctx)
	if err != nil {
		panic(err)
	}
	return id
}

// RegionTypeCreateBulk is the builder for creating many RegionType entities in bulk.
type RegionTypeCreateBulk struct {
	config
	err      error
	builders []*RegionTypeCreate
	conflict []sql.ConflictOption
}

// Save creates the RegionType entities in the database.
func (rtcb *RegionTypeCreateBulk) Save(ctx context.Context) ([]*RegionType, error) {
	if rtcb.err != nil {
		return nil, rtcb.err
	}
	specs := make([]*sqlgraph.CreateSpec, len(rtcb.builders))
	nodes := make([]*RegionType, len(rtcb.builders))
	mutators := make([]Mutator, len(rtcb.builders))
	for i := range rtcb.builders {
		func(i int, root context.Context) {
			builder := rtcb.builders[i]
			var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) {
				mutation, ok := m.(*RegionTypeMutation)
				if !ok {
					return nil, fmt.Errorf("unexpected mutation type %T", m)
				}
				if err := builder.check(); err != nil {
					return nil, err
				}
				builder.mutation = mutation
				var err error
				nodes[i], specs[i] = builder.createSpec()
				if i < len(mutators)-1 {
					_, err = mutators[i+1].Mutate(root, rtcb.builders[i+1].mutation)
				} else {
					spec := &sqlgraph.BatchCreateSpec{Nodes: specs}
					spec.OnConflict = rtcb.conflict
					// Invoke the actual operation on the latest mutation in the chain.
					if err = sqlgraph.BatchCreate(ctx, rtcb.driver, spec); err != nil {
						if sqlgraph.IsConstraintError(err) {
							err = &ConstraintError{msg: err.Error(), wrap: err}
						}
					}
				}
				if err != nil {
					return nil, err
				}
				mutation.id = &nodes[i].ID
				if specs[i].ID.Value != nil && nodes[i].ID == 0 {
					id := specs[i].ID.Value.(int64)
					nodes[i].ID = int64(id)
				}
				mutation.done = true
				return nodes[i], nil
			})
			for i := len(builder.hooks) - 1; i >= 0; i-- {
				mut = builder.hooks[i](mut)
			}
			mutators[i] = mut
		}(i, ctx)
	}
	if len(mutators) > 0 {
		if _, err := mutators[0].Mutate(ctx, rtcb.builders[0].mutation); err != nil {
			return nil, err
		}
	}
	return nodes, nil
}

// SaveX is like Save, but panics if an error occurs.
func (rtcb *RegionTypeCreateBulk) SaveX(ctx context.Context) []*RegionType {
	v, err := rtcb.Save(ctx)
	if err != nil {
		panic(err)
	}
	return v
}

// Exec executes the query.
func (rtcb *RegionTypeCreateBulk) Exec(ctx context.Context) error {
	_, err := rtcb.Save(ctx)
	return err
}

// ExecX is like Exec, but panics if an error occurs.
func (rtcb *RegionTypeCreateBulk) ExecX(ctx context.Context) {
	if err := rtcb.Exec(ctx); err != nil {
		panic(err)
	}
}

// OnConflict allows configuring the `ON CONFLICT` / `ON DUPLICATE KEY` clause
// of the `INSERT` statement. For example:
//
//	client.RegionType.CreateBulk(builders...).
//		OnConflict(
//			// Update the row with the new values
//			// the was proposed for insertion.
//			sql.ResolveWithNewValues(),
//		).
//		// Override some of the fields with custom
//		// update values.
//		Update(func(u *ent.RegionTypeUpsert) {
//			SetType(v+v).
//		}).
//		Exec(ctx)
func (rtcb *RegionTypeCreateBulk) OnConflict(opts ...sql.ConflictOption) *RegionTypeUpsertBulk {
	rtcb.conflict = opts
	return &RegionTypeUpsertBulk{
		create: rtcb,
	}
}

// OnConflictColumns calls `OnConflict` and configures the columns
// as conflict target. Using this option is equivalent to using:
//
//	client.RegionType.Create().
//		OnConflict(sql.ConflictColumns(columns...)).
//		Exec(ctx)
func (rtcb *RegionTypeCreateBulk) OnConflictColumns(columns ...string) *RegionTypeUpsertBulk {
	rtcb.conflict = append(rtcb.conflict, sql.ConflictColumns(columns...))
	return &RegionTypeUpsertBulk{
		create: rtcb,
	}
}

// RegionTypeUpsertBulk is the builder for "upsert"-ing
// a bulk of RegionType nodes.
type RegionTypeUpsertBulk struct {
	create *RegionTypeCreateBulk
}

// UpdateNewValues updates the mutable fields using the new values that
// were set on create. Using this option is equivalent to using:
//
//	client.RegionType.Create().
//		OnConflict(
//			sql.ResolveWithNewValues(),
//			sql.ResolveWith(func(u *sql.UpdateSet) {
//				u.SetIgnore(regiontype.FieldID)
//			}),
//		).
//		Exec(ctx)
func (u *RegionTypeUpsertBulk) UpdateNewValues() *RegionTypeUpsertBulk {
	u.create.conflict = append(u.create.conflict, sql.ResolveWithNewValues())
	u.create.conflict = append(u.create.conflict, sql.ResolveWith(func(s *sql.UpdateSet) {
		for _, b := range u.create.builders {
			if _, exists := b.mutation.ID(); exists {
				s.SetIgnore(regiontype.FieldID)
			}
		}
	}))
	return u
}

// Ignore sets each column to itself in case of conflict.
// Using this option is equivalent to using:
//
//	client.RegionType.Create().
//		OnConflict(sql.ResolveWithIgnore()).
//		Exec(ctx)
func (u *RegionTypeUpsertBulk) Ignore() *RegionTypeUpsertBulk {
	u.create.conflict = append(u.create.conflict, sql.ResolveWithIgnore())
	return u
}

// DoNothing configures the conflict_action to `DO NOTHING`.
// Supported only by SQLite and PostgreSQL.
func (u *RegionTypeUpsertBulk) DoNothing() *RegionTypeUpsertBulk {
	u.create.conflict = append(u.create.conflict, sql.DoNothing())
	return u
}

// Update allows overriding fields `UPDATE` values. See the RegionTypeCreateBulk.OnConflict
// documentation for more info.
func (u *RegionTypeUpsertBulk) Update(set func(*RegionTypeUpsert)) *RegionTypeUpsertBulk {
	u.create.conflict = append(u.create.conflict, sql.ResolveWith(func(update *sql.UpdateSet) {
		set(&RegionTypeUpsert{UpdateSet: update})
	}))
	return u
}

// SetType sets the "type" field.
func (u *RegionTypeUpsertBulk) SetType(v string) *RegionTypeUpsertBulk {
	return u.Update(func(s *RegionTypeUpsert) {
		s.SetType(v)
	})
}

// UpdateType sets the "type" field to the value that was provided on create.
func (u *RegionTypeUpsertBulk) UpdateType() *RegionTypeUpsertBulk {
	return u.Update(func(s *RegionTypeUpsert) {
		s.UpdateType()
	})
}

// Exec executes the query.
func (u *RegionTypeUpsertBulk) Exec(ctx context.Context) error {
	if u.create.err != nil {
		return u.create.err
	}
	for i, b := range u.create.builders {
		if len(b.conflict) != 0 {
			return fmt.Errorf("ent: OnConflict was set for builder %d. Set it on the RegionTypeCreateBulk instead", i)
		}
	}
	if len(u.create.conflict) == 0 {
		return errors.New("ent: missing options for RegionTypeCreateBulk.OnConflict")
	}
	return u.create.Exec(ctx)
}

// ExecX is like Exec, but panics if an error occurs.
func (u *RegionTypeUpsertBulk) ExecX(ctx context.Context) {
	if err := u.create.Exec(ctx); err != nil {
		panic(err)
	}
}
